﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using OLTS.WEB.BaseClass;
using OLTS.MODEL;
using OLTS.DATA_SERVICE;
using System.Web.Services;
using System.Data;
namespace ONTS.WEB.Views
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    if (Session["LOGIN"] == null)
                    {
                        //Response.Redirect("Default.aspx");
                    }
                }

                OTS_LOGIN model = (OTS_LOGIN)Session["LOGIN"];
                if (model != null)
                {
                    lbLogin.Text = model.OTS_EMPLOYEE.NAME_EN + " [" + model.ROLE + "]";
                    var role = model.ROLE.Split(':');
                    if (role.Contains("administrator"))
                    {
                        GenMenu(Page.MapPath("../menu/AdminMenu.xml"));
                    } 
                    else
                    {
                        var format = BaseService.PROJECT.OTS_SITE.TIMESHEET_FORMAT;
                        if (format == "1")
                        {
                            GenMenu(Page.MapPath("../menu/EmployeeBOT.xml"));
                        }
                        else if (format == "2")
                        {
                            GenMenu(Page.MapPath("../menu/EmployeeCUBE.xml"));
                        }
                        else if (format == "3")
                        {
                            GenMenu(Page.MapPath("../menu/EmployeeTbank.xml"));
                        }
                        else
                        {
                            //Response.Redirect("AccessDeniedPage.aspx");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Response.Redirect("AccessDeniedPage.aspx");
            }

        }

        private void GenMenu(String path)
        {
            sidebar.Controls.Add(new LiteralControl("<script type=\"text/javascript\">"));
            sidebar.Controls.Add(new LiteralControl("try { ace.settings.check('sidebar', 'fixed') } catch (e) { }"));
            sidebar.Controls.Add(new LiteralControl("</script>"));
            sidebar.Controls.Add(new LiteralControl("<ul class=\"nav nav-list\">"));
            XDocument coordinates = XDocument.Load(path);
            int i = 0;
            foreach (var coordinate in coordinates.Descendants("submenu"))
            {
                i = i + 1;
                string href = coordinate.Element("href").Value;
                string icon = coordinate.Element("icon").Value;
                string text = coordinate.Element("text").Value;

                sidebar.Controls.Add(new LiteralControl("<li id='m" + i + "' class=\"\">"));
                sidebar.Controls.Add(new LiteralControl("<a href=" + href + ">"));
                sidebar.Controls.Add(new LiteralControl("<i class=" + icon + "></i>"));
                sidebar.Controls.Add(new LiteralControl("<span class=\"menu-text\">" + text + "</span>"));
                sidebar.Controls.Add(new LiteralControl("</a>"));
                sidebar.Controls.Add(new LiteralControl("<b class=\"arrow\"></b>"));
                sidebar.Controls.Add(new LiteralControl("</li>"));
            }
            sidebar.Controls.Add(new LiteralControl("</ul>"));
        }
    }
}